from starlette.endpoints import HTTPEndpoint
from starlette.responses import JSONResponse
from model.ModDepWorkloadRepStation import DepWorkloadRepStation, depWorkloadRepStationSchema
from model.ModDepWorkloadRep import DepWorkloadRep, depWorkloadRepSchema
from database import Session, engine
import datetime


def get_dep_workload_rep(session, begin_time: datetime, end_time: datetime, openid: str, bureauid: str):
    # 项目部工作量统计表
    connection = engine.raw_connection()
    try:
        cursor = connection.cursor()
        cursor.callproc("statics_dep_work", (begin_time, end_time, openid))
        cursor.close()
        connection.commit()
        if bureauid == "00":
            dep_workload = session.query(DepWorkloadRep).filter(
                DepWorkloadRep.openid == openid).all()
            return JSONResponse(depWorkloadRepSchema.dump(dep_workload, many=True).data)
        else:
            dep_workload = session.query(DepWorkloadRep).filter(DepWorkloadRep.bureauid == bureauid).filter(
                DepWorkloadRep.openid == openid).all()
            return JSONResponse(depWorkloadRepSchema.dump(dep_workload, many=True).data)
    finally:
        connection.close()

def get_dep_workload_rep_station(session, begin_time: datetime, end_time: datetime, openid: str, bureauid: str):
    # 项目部工作量统计表
    connection = engine.raw_connection()
    try:
        cursor = connection.cursor()
        cursor.callproc("statics_dep_work_station", (begin_time, end_time, openid, bureauid))
        cursor.close()
        connection.commit()
        dep_workload = session.query(DepWorkloadRepStation).filter(DepWorkloadRepStation.bureauid == bureauid).filter(
            DepWorkloadRepStation.openid == openid).order_by(DepWorkloadRepStation.problem_number.desc()).all()
        return JSONResponse(depWorkloadRepStationSchema.dump(dep_workload, many=True).data)
    finally:
        connection.close()
